#import "sprite_game_enemy_boss2.h"

@implementation SpriteGameEnemyBoss2

- (id)initSpriteGameEnemyBoss2
{
	if ((self = [super initSpriteGameEnemy]))
	{
		objectName_ = "object sprite game enemy type boss 2";
		
		/*box2d
		*/
		fixtureBody_ = NULL;
	}
	return self;
}

- (void)tickBoss2StageChangeSkullReturn:(ccTime)Time
{
	int i;
	float _Angle;
	CGPoint _Position;
	std::list<sprite_game_enemy_skull *>::iterator _Iter;

	[self unschedule:@selector(tickBoss2StageChangeSkullReturn:)];
	_Angle = 360 / skullList_.size();
	_Iter = skullList_.begin();
	for (i = 0; _Iter != skullList_.end(); _Iter++, i++)
	{
		_Position = ptRotatePoint_CGPoint(ccp(0.0f, 230.0f), i * _Angle);
		[*_Iter runAction:[CCShow action]];
		[*_Iter ActionOut:1.0f And:_Position];
	}
	[self schedule:@selector(tickBoss2StageChangeSkullOut:) interval:1.2f];
}

- (void)tickBoss2StageChangeSkullOut:(ccTime)Time
{
	[self unschedule:@selector(tickBoss2StageChangeSkullOut:)];
	[self ActionAttack];
	[self StartTimerBoss2StartStrongAttack];

	/*music
	*/
	switch (flagBoss2Stage_)
	{
	case BOSS_2_STAGE_2: MusicPlayGameBossStage2(); break;
	case BOSS_2_STAGE_3: MusicPlayGameBossStage3(); break;
	}
}

- (void)tickBoss2StartStrongAttack:(ccTime)Time
{
	int i;
	int _SkullIndex;
	std::list<sprite_game_enemy_skull *>::iterator _Iter;

	state_ = ENEMY_STATE_BOSS2_STRONG_ATTACK;
	[self StopAction];
	[self PlayAni:ENEMY_ANI_MOVE];
	[self runAction:[action_move_in_pixel actionWithDuration:2.0f position:g_CfgGameScrrenArea[4].center]];
	_SkullIndex = rand() % skullList_.size();
	_Iter = skullList_.begin();
	for (i = 0; i < _SkullIndex; ++i)
	{
		_Iter++;
	}
	switch (flagBoss2Stage_)
	{
	case BOSS_2_STAGE_1:[(*_Iter) ActionWeakPoint:6]; break;
	case BOSS_2_STAGE_2:[(*_Iter) ActionWeakPoint:5]; break;
	case BOSS_2_STAGE_3:[(*_Iter) ActionWeakPoint:4]; break;
	default:;
	}
	[self EndTimerBoss2StartStrongAttack];
}

- (void)tickBoss2BlackHole:(ccTime)Time
{
	int i;
	sprite_game_swirl *_Swirl[2];

	for (i = 0; i < 2; ++i)
	{
		_Swirl[i] = [[sprite_game_swirl alloc] initSpriteGameSwirl:SWIRL_TYPE_0];
		[g_LayerGameObject AddSwirl:_Swirl[i]];
		_Swirl[i].positionInPixels = CfgGameGetPointCGPoint(i);
		[_Swirl[i] ActionAppear];
	}
}

- (void)createBody
{
	int i;
	int _VertexCount;
	float _Ratio;
	b2Vec2 *_Vertex;
	b2PolygonShape _PolygonShape;
	b2FixtureDef _FixtureDef;

	[super createBody];

	_Ratio = 1 / CMgrGamePhysics::Ratio();
	_FixtureDef.density = 1.0f;
	_FixtureDef.isSensor = true;

	/*body
	*/
	_VertexCount = g_CfgGameBoss2Data.box2d_collision_point_table_body.size();
	_Vertex = ptCreateCollisionVertex(&g_CfgGameBoss2Data.box2d_collision_point_table_body);
	for (i = 0; i < _VertexCount; ++i)
	{
		_Vertex[i] = ptScaleVector_same(_Vertex[i], _Ratio);
	}
	_PolygonShape.Set(_Vertex, _VertexCount);
	free(_Vertex);
	_FixtureDef.shape = &_PolygonShape;
	fixtureBody_ = box2dBody_->CreateFixture(&_FixtureDef);
	_FixtureDef.shape = &_PolygonShape;
	fixtureBoss3Head_ = box2dBody_->CreateFixture(&_FixtureDef);
}

- (void)destroyBody
{
	[super destroyBody]
	fixtureBody_ = NULL;
}

- (void)StartTimerBoss2StartStrongAttack
{
	switch (flagBoss2Stage_)
	{
	case BOSS_2_STAGE_1:[self schedule:@selector(tickBoss2StartStrongAttack:) interval:15.0f]; break;
	case BOSS_2_STAGE_2:[self schedule:@selector(tickBoss2StartStrongAttack:) interval:12.0f]; break;
	case BOSS_2_STAGE_3:[self schedule:@selector(tickBoss2StartStrongAttack:) interval:10.0f]; break;
	default:;
	}
}

- (void)EndTimerBoss2StartStrongAttack
{
	[self unschedule:@selector(tickBoss2StartStrongAttack:)];
}

- (void)ActionBoss2Appear
{
	CCSequence *_Seq;
	id _ActionMove;
	CCCallFuncN *_CallBackAppearMoveOver;

	if (state_ == ENEMY_STATE_DIE)
	{
		return;
	}

	state_ = ENEMY_STATE_BOSS2_APPEAR;
	[self PlayAni:ENEMY_ANI_MOVE];
	_ActionMove = tcCreateActionMove(self.positionInPixels, g_CfgGameScrrenArea[4].center, [self GetMoveSpeed]);
	_CallBackAppearMoveOver = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackBoss2ActionAppearOver:)];
	_Seq = [CCSequence actions:_ActionMove, _CallBackAppearMoveOver, nil];
	[self runAction:_Seq];
}

- (void)ActionBoss2Stage2
{
	sprite_game_swirl *_Swirl;

	if (state_ == ENEMY_STATE_DIE)
	{
		return;
	}

	[self ActionBoss2StageChange:5];
	_Swirl = [[sprite_game_swirl alloc] initSpriteGameSwirl:SWIRL_TYPE_0];
	[g_LayerGameObject AddSwirl:_Swirl];
	_Swirl.positionInPixels = CfgGameGetPointCGPoint(0);
	[_Swirl ActionAppear];
	flagBoss2Stage_ = BOSS_2_STAGE_2;
}

- (void)ActionBoss2Stage3
{
	int i;
	sprite_game_swirl *_Swirl[2];

	if (state_ == ENEMY_STATE_DIE)
	{
		return;
	}

	[self ActionBoss2StageChange:7];
	for (i = 0; i < 2; ++i)
	{
		_Swirl[i] = [[sprite_game_swirl alloc] initSpriteGameSwirl:SWIRL_TYPE_0];
		[g_LayerGameObject AddSwirl:_Swirl[i]];
		_Swirl[i].positionInPixels = CfgGameGetPointCGPoint(i);
		[_Swirl[i] ActionAppear];
	}
	flagBoss2Stage_ = BOSS_2_STAGE_3;

	[self schedule:@selector(tickBoss2BlackHole:) interval:25];
}

- (void)ActionBoss2StageChange:(int)SkullCount
{
	int i;
	std::list<sprite_game_enemy_skull *>::iterator _Iter;
	sprite_game_enemy_skull *_Skull;

	if (state_ == ENEMY_STATE_DIE)
	{
		return;
	}

	state_ = ENEMY_STATE_BOSS2_STAGE_CHANGE;
	[self StopAction];
	[self PlayAni:ENEMY_ANI_MOVE];
	_Iter = skullList_.begin();
	for (; _Iter != skullList_.end(); ++_Iter)
	{
		[*_Iter ActionReturn:1.0f];
		[*_Iter StopActionWeakPoint];
	}
	for (i = 0; i < skullList_.size() - SkullCount; ++i)
	{
		_Skull = [self AddSkull:CGPointZero And:FALSE];
		[_Skull runAction:[CCHide action]];
		_Skull.scale = 0.0f;
	}
	[self schedule:@selector(tickBoss2StageChangeSkullReturn:) interval:1.2f];
	[self EndTimerBoss2StartStrongAttack];
}

- (void)ActionBoss2StrongAttack:(int)BulletCount
{
	int i;
	float _X;
	float _Time;
	CGPoint _StartPos, _EndPos;
	sprite_game_bomb *_Bomb;

	if (state_ == ENEMY_STATE_DIE)
	{
		return;
	}

	state_ = ENEMY_STATE_BOSS2_STRONG_ATTACK;
	[self StopAction];
	[self PlayAni:ENEMY_ANI_ATTACK];
	_X = self.positionInPixels.x - (BulletCount / 2 * 100 + (BulletCount % 2 == 0 ? 50 : 0));
	_StartPos = self.positionInPixels;
	for (i = 0; i < BulletCount; ++i, _X += 100)
	{
		_Bomb = [[sprite_game_bomb alloc] initSpriteGameBomb:BOMB_TYPE_1];
		[g_LayerGameObject AddBomb:_Bomb];
		_EndPos = [g_SpriteGameBridge GetBridgePositionByX:_X];
		_Bomb.positionInPixels = _StartPos;
		_Time = ptPointDistance_CGPoint(self.positionInPixels, _EndPos) / 90.0f;
		[_Bomb ActionAttack:_Time And:_EndPos];
	}
	[self StartTimerBoss2StartStrongAttack];
}

@end